import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HeaderDate extends StatefulWidget {
  @override
  _HeaderDateState createState() => _HeaderDateState();
}

class _HeaderDateState extends State<HeaderDate> {
  List _dateList = <DateTime>[
    DateTime.now().subtract(Duration(days: 1)),
    DateTime.now(),
    DateTime.now().add(Duration(days: 1))
  ];
  int _currentIndex = 1;

  @override
  void initState() {
    super.initState();
    print(1111);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      // child: Swiper(
      //   loop: false,
      //   index: _currentIndex,
      //   itemBuilder: (BuildContext context, int index) {
      //     return Center(
      //       child: Text('${_dateList[index]}'),
      //     );
      //   },
      //   itemCount: 3,
      //   control: SwiperControl(
      //       color: Colors.grey, padding: EdgeInsets.fromLTRB(40, 0.0, 40, 0.0)),
      //   onIndexChanged: (int index) {
      //     if (_currentIndex - index > 0) {
      //       DateTime currentDate = _dateList[index];
      //       _dateList.removeAt(2);
      //       _dateList.insert(0, currentDate.subtract(Duration(days: 1)));
      //       index++;
      //       print(currentDate);
      //     } else {
      //       print('向左');
      //     }
      //     setState(() {
      //       _currentIndex = index;
      //     });

      //     print(_currentIndex);
      //   },
      // ),
      child: PageView(
        controller: PageController(
          initialPage: _currentIndex
        ),
        children: <Widget>[
          Text('${_dateList[0]}'),
          Text('${_dateList[1]}'),
          Text('${_dateList[2]}'),
        ],
        onPageChanged: (index){
          List dates;
          if(index < _currentIndex){
            print('left');
            dates = _dateList;
            DateTime currentDate = dates[index];
            dates.removeAt(2);
            dates.insert(0, currentDate.subtract(Duration(days: 1)));
          }else{
            print('right');
          }
          setState(() {
            _dateList = dates;
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
